package com.wk.contrller;

//import com.fasterxml.jackson.databind.ObjectMapper;

import com.wk.bean.WkUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller

public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {
        return "哈喽";//是返回aaaa当页面还是文本输出
    }

    //@ResponseBody
    @RequestMapping("/")
    public String index() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("login");
        return "login";
    }

    /*@ResponseBody
    @RequestMapping(value="/myEndpoint")
    public Object handleRequest(HttpServletResponse response) throws IOException {

            // 在使用 Jackson-databind 进行数据转换之前，设置响应的 Content-Type
            response.setContentType("application/json"); // 设置 Content-Type 为 application/json
            // 使用 Jackson-databind 进行数据转换
            ObjectMapper mapper = new ObjectMapper();
            String jsonString = "{\"key\":\"value\"}";
            Object object = mapper.readValue(jsonString, Object.class);
            // 处理转换后的数据并返回响应
             return object;

    }*/


    //PostMapping("/users")
    @RequestMapping(value = "/users", method = RequestMethod.POST)
    @ResponseBody
    public String save(WkUser user) {
        System.out.println("user save..." + user);
        return "{'module':'user save'}";
    }

    /**
     * @PathVariable 映射 URL 绑定的占位符
     * 通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过
     *
     * @PathVariable(“xxx”) 绑定到操作方法的入参中。
     * @param id
     * @return
     */
    //DeleteMapping("/users/{id}")
    @RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable Integer id) {
        System.out.println("user delete..." + id);
        return "{'module':'user delete'}";
    }

   // @PutMapping("/users")
    @RequestMapping(value = "/users", method = RequestMethod.PUT)
    @ResponseBody
    public String update(@RequestBody WkUser user) {
        System.out.println("user update..." + user);
        return "{'module':'user update'}";
    }

   // @GetMapping("/users/{id}")
    @RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String getById(@PathVariable Integer id) {
        System.out.println("user getById..." + id);
        return "{'module':'user getById'}";
    }
    //@GetMapping("/users")
    @RequestMapping(value = "/users", method = RequestMethod.GET)
    @ResponseBody
    public String getAll() {
        System.out.println("user getAll...");
        return "{'module':'user getAll'}";
    }


}
